
//
//  FMSQLiteManager.m
//  WeEsion
//
//  Created by reylen on 15/12/24.
//  Copyright © 2015年 fdauto. All rights reserved.
//

#import "FMSQLiteManager.h"

@implementation FMSQLiteManager

+ (FMSQLiteManager *)sharedManager
{
    static FMSQLiteManager *sharedSQLiteManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedSQLiteManagerInstance = [[self alloc] init];
    });
    return sharedSQLiteManagerInstance;
}

- (BOOL) openSQLiteDBWithPath:(NSString *) filePath {
    
    if (sqlite3_open([filePath UTF8String], &db)==SQLITE_OK) {
        return YES;
    }
    
    return NO;
}

- (void) closeSQLiteDB {
    sqlite3_close(db);
}

// 查询数据库所有表名
- (NSArray *) queryTableFromDB {
    
    sqlite3_stmt *statement;
    
    NSString* sqlStr = [NSString stringWithFormat:@"select * from sqlite_master where type='table' order by name"];
    const char* sql = [sqlStr UTF8String];
    if (sqlite3_prepare_v2(db, sql, -1, &statement, nil)==SQLITE_OK) {
        NSLog(@"select ok.");
    }
    
    NSMutableArray* results = [NSMutableArray array];
    while (sqlite3_step(statement)==SQLITE_ROW) {
        int _id=sqlite3_column_int(statement, 0);
        char *name=(char *)sqlite3_column_text(statement, 1);
        NSLog(@"row>>id %i, name %s",_id,name);
        [results addObject:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
    }
    
    sqlite3_finalize(statement);
    
    return results;
}

// 查询表所有属性字段名
- (NSArray *) queryPragmaFromTable:(NSString *) table {
    sqlite3_stmt *statement;
    
    NSString* sqlStr = [NSString stringWithFormat:@"PRAGMA table_info(%@)",table];
    const char* sql = [sqlStr UTF8String];
    if (sqlite3_prepare_v2(db, sql, -1, &statement, nil)==SQLITE_OK) {
        NSLog(@"select ok.");
    }
    
    NSMutableArray* results = [NSMutableArray array];
    while (sqlite3_step(statement)==SQLITE_ROW) {
        
        char *nameData = (char *)sqlite3_column_text(statement, 1);
        NSLog(@"columnName:%s",nameData);
        
        [results addObject:[NSString stringWithCString:nameData encoding:NSUTF8StringEncoding]];
    }
    
    sqlite3_finalize(statement);
    
    return results;
}

// 查询表数据
- (NSArray *) queryWithSQL:(NSString *) sqlStr {
    
    sqlite3_stmt *statement;
    
    const char* sql = [sqlStr UTF8String];
    if (sqlite3_prepare_v2(db, sql, -1, &statement, nil)==SQLITE_OK) {
        NSLog(@"select ok.");
    }
    
    NSMutableArray* results = [NSMutableArray array];
    while (sqlite3_step(statement)==SQLITE_ROW) {
        
        int count = sqlite3_column_count(statement);
        NSMutableDictionary* info = [NSMutableDictionary dictionary];
        for (int i = 0; i < count; i ++) {
            char *result =(char *)sqlite3_column_text(statement, i);
            NSString* value = nil;
            if (result != NULL) {
                value = [NSString stringWithCString:result encoding:NSUTF8StringEncoding];
            }
            
            const char *columnName = sqlite3_column_name(statement, i);
            NSString* key = [NSString stringWithUTF8String:columnName];
            
            if (value == nil) {
                value = @"";
            }
            
            [info setObject:value forKey:key];
        }
        
        [results addObject:info];
    }
    
    sqlite3_finalize(statement);
    
    return results;
}

@end
